﻿using AutoMapper;
using B.S.RbacData.Domain;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys;
using B.S.RbacData.Read.API.Dtos.Traceabilitys;
using MediatR;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys
{
    /// <summary>
    /// 获取产品信息命令处理
    /// </summary>
    public class GetProductCommandHandlter : IRequestHandler<GetProductCommand, ApiPaging<GetProductDtos>>
    {
        /// <summary>
        /// 产品信息
        /// </summary>
        private readonly IBaseRepository<ProductModel> productRep;
        /// <summary>
        /// automapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// http上下文
        /// </summary>
        private readonly IHttpContextAccessor httpContextAccessor;
        /// <summary>
        /// 商品信息
        /// </summary>
        private readonly IBaseRepository<GoodsModel> goodsRep;
        private readonly IBaseRepository<UserModel> userRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="productRep">产品信息</param>
        /// <param name="mapper">automapper映射</param>
        /// <param name="httpContextAccessor">http上下文</param>
        public GetProductCommandHandlter(IBaseRepository<ProductModel> productRep, IMapper mapper, IHttpContextAccessor httpContextAccessor, IBaseRepository<GoodsModel> goodsRep,IBaseRepository<UserModel> userRep)
        {
            this.productRep = productRep;
            this.mapper = mapper;
            this.httpContextAccessor = httpContextAccessor;
            this.goodsRep = goodsRep;
            this.userRep = userRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiPaging<GetProductDtos>> Handle(GetProductCommand request, CancellationToken cancellationToken)
        {


            //获取产品所有信息
            var produtlist = from a in productRep.GetAll().Where(x =>!x.IsDeleted).Include(x => x.Goods)
                             join b in goodsRep.GetAll()
                             on a.GoodsId equals b.ID
                             select new GetProductDtos()
                             {
                                 Completeness = a.Completeness,
                                 CoverImage = a.CoverImage,
                                 CreateTime =a.CreateTime,
                                 CreateUserName = userRep.GetAll().FirstOrDefault(x=>x.ID==a.CreateUserId).UserName,
                                 GoodsId = a.GoodsId,
                                 UpdateUserName = userRep.GetAll().FirstOrDefault(x => x.ID == a.UpdateUserId).UserName,
                                 Manufacturer = a.Manufacturer,
                                 ProductionBatch = a.ProductionBatch,
                                 ProductionLicenseNumber = a.ProductionLicenseNumber,
                                 ShelfLifeDay = a.ShelfLifeDay,
                                 ID = a.ID,
                                 GoodsName = a.Goods.GoodsName,
                       
                             };
            #region 条件查询
            //根据商品名称模糊查询
            if (!string.IsNullOrEmpty(request.GoodsName))
            {
                produtlist= produtlist.Where(x=>x.GoodsName.Contains(request.GoodsName));

            }

            //根据修改人模糊查询
            if (!string.IsNullOrEmpty(request.UpdateUserName))
            {
                produtlist = produtlist.Where(x => x.UpdateUserName.Contains(request.UpdateUserName));

            }

            //根据创建人模糊查询
            if (!string.IsNullOrEmpty(request.CreateUserName))
            {
                produtlist = produtlist.Where(x => x.CreateUserName.Contains(request.CreateUserName));

            }
            //根据生产批次模糊查询
            if (!string.IsNullOrEmpty(request.ProductionBatch))
            {
                produtlist = produtlist.Where(x => x.ProductionBatch.Contains(request.ProductionBatch));

            }
            //根据生产许可证号模糊查询
            if (!string.IsNullOrEmpty(request.ProductionLicenseNumber))
            {
                produtlist = produtlist.Where(x => x.ProductionLicenseNumber.Contains(request.ProductionLicenseNumber));

            }
            //根据生产企业模糊查询
            if (!string.IsNullOrEmpty(request.Manufacturer))
            {
                produtlist = produtlist.Where(x => x.Manufacturer.Contains(request.Manufacturer));

            }
            //根据保质期精确查询
            if (!string.IsNullOrEmpty(request.ShelfLifeDay))
            {
                produtlist = produtlist.Where(x => x.ShelfLifeDay==Convert.ToInt32(request.ShelfLifeDay));

            }
            //根据创建时间模糊查询
            if (!string.IsNullOrEmpty(request.CreateTime))
            {
                produtlist = produtlist.Where(x => x.CreateTime>=Convert.ToDateTime(request.CreateTime)&&x.CreateTime<= Convert.ToDateTime(request.CreateTime).AddDays(1));

            }

            #endregion
            //总条数
            var totalcount = produtlist.Count();
            //总页数
            var totalpage = (int)Math.Ceiling(1.0 * totalcount / request.PageSize);

            //分页
            var list = produtlist.OrderByDescending(x => x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            var resutlist = mapper.Map<List<GetProductDtos>>(list);

            return new ApiPaging<GetProductDtos>()
            {
                code = ResultEnumCode.Ok,
                message = "获取成功",
                PageData = resutlist,
                TotalCount = totalcount,
                PageCount = totalpage,
            };
        }
    }
}
